using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMud.Text.Dynamic.Processing.Metacommands
{
    public class Import : IMetacommand
    {
        #region Private Variables
        private const string MetacommandName = "Import";
        IList<KeyValuePair<string, string>> _Parameters;
        #endregion

        #region Constructors
        private Import()
        {
            _Parameters = new List<KeyValuePair<string, string>>();
        }
        #endregion

        #region Public Properties
        public string Namespace
        {
            get
            {
                return GetValueForKey("Namespace");
            }
            set
            {
                SetValueAtKey("Namespace", value);
            }
        }
        #endregion

        #region Private Methods
        private string GetValueForKey(string key)
        {
            string k = key.ToLower().Trim();
            foreach (KeyValuePair<string, string> kvp in _Parameters)
            {
                if (kvp.Key.ToLower().Trim() == k)
                {
                    return kvp.Value;
                }
            }
            return null;
        }
        private void SetValueAtKey(string key, string value)
        {
            string k = key.ToLower().Trim();
            foreach (KeyValuePair<string, string> kvp in _Parameters)
            {
                if (kvp.Key.ToLower().Trim() == k)
                {
                    int i = _Parameters.IndexOf(kvp);
                    _Parameters.RemoveAt(i);
                    _Parameters.Insert(i, new KeyValuePair<string, string>(kvp.Key, value));
                    return;
                }
                _Parameters.Add(new KeyValuePair<string, string>(key, value));
            }

        }
        #endregion

        #region IMetacommand Members

        string IMetacommand.Name
        {
            get { return MetacommandName; }
        }

        IList<KeyValuePair<string, string>> IMetacommand.Parameters
        {
            get { return _Parameters; }
        }

        #endregion

        public static Import FromMetacommand(IMetacommand metacommand)
        {
            if (metacommand.Name.Trim().ToLower() != MetacommandName.Trim().ToLower())
                throw new ArgumentException("Incompatible metacommand not acceptable.");
            Import i = new Import();
            i._Parameters = metacommand.Parameters;
            return i;
        }
    }
}
